package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.tree.DefaultMutableTreeNode;

import controller.Controller;
import po.Room;
import po.Student;

public class Frame extends JFrame {
	private Controller controller;
	private JTree tree;
	private JScrollPane tablePanel;
	private JPanel jp;
	private List<Room> roomNameList;
	public Frame(LoginPanel loginPanel) {
		dispose(); // 当前窗口消失
		this.controller = loginPanel.getController();// LoginPanel和Frame共用同一个controller
		this.setJMenuBar(getContactsJMenuBar());
		this.add(this.treeWin(controller.getContactsService().findAllRoom()), BorderLayout.WEST);
		this.add(getToolPanel(),BorderLayout.NORTH);
		this.add(this.getBookTablePanel(controller.getContactsService().findAll()), BorderLayout.CENTER);
//		this.setLayout(new GridLayout(1,4));
		this.setVisible(true);
		this.setSize(1215, 500);
		this.setResizable(true);// 可以调整窗口大小
		this.setLocationRelativeTo(null);// 将窗口放在屏幕正中心
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.validate();
	}
	public JMenuBar getContactsJMenuBar() {
		JMenuItem allContacts = new JMenuItem("所有人的通讯录");// 菜单列表上的按钮
		allContacts.addActionListener(controller);
		allContacts.setActionCommand("findAll");
		JMenu menu = new JMenu("通讯录");// 创建菜单
		JMenuBar a = new JMenuBar();// 创建菜单条
		a.add(menu);
		menu.add(allContacts);
		return a;
	}

	/**
	 * @return 创建树组件
	 */
	public JPanel treeWin(List<Room> roomNameList) {
		this.roomNameList = roomNameList;
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("学生通讯录");// 根结点
		DefaultMutableTreeNode roomNo = new DefaultMutableTreeNode("宿舍号查询");
		Object data1[] = new Object[roomNameList.size()];
		Iterator<Room> iter = roomNameList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Room r = iter.next();
			data1[i] = r.getRoomName();				
			DefaultMutableTreeNode room = new DefaultMutableTreeNode(data1[i]);
			roomNo.add(room);	
			i++;
		}				
		root.add(roomNo);//// 确定结点之间的联系
		tree = new JTree(root);// 用root做根的树组件
		tree.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		jp = new JPanel(new BorderLayout());
		jp.add(tree);
		jp.setPreferredSize(new Dimension(125, 500));// 设置面板的宽度 高度
		tree.addTreeSelectionListener(controller);// 监听树组件上的事件
		return jp;

	}

	/**
	 * @return 滚动窗格和表格组件
	 */
	public JScrollPane getBookTablePanel(List<Student> studentList) {
		Object dataName[] = { "学号", "姓名", "生日", "联系方式", "家庭住址", "邮箱", "房名" };
		Object data[][] = new Object[studentList.size()][7];
		Iterator<Student> iter = studentList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Student stu = iter.next();
			data[i][0] = stu.getStudentId();
			data[i][1] = stu.getStudentName();
			data[i][2] = stu.getStudentBirthday();
			data[i][3] = stu.getStudentTelephoneNumber();
			data[i][4] = stu.getStudentAddress();
			data[i][5] = stu.getStudentEmail(); 
			data[i][6] = stu.getRoom().getRoomName();
			i++;
		}
		JTable table = new JTable(data, dataName);//表格组件
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 16)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 16)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(10, 30)); // 表头高度 先后为宽 高
		table.setPreferredScrollableViewportSize(new Dimension(1060,1000));//设置表的大小
		table.setRowHeight(table.getRowHeight() + 10); // 表体高度
		table.setRowMargin(5);//设置相邻行中的单元格之间的空白空间量
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		tablePanel = new JScrollPane(table);
		return tablePanel;
	}
	/**
	 * @return
	 * 工具栏
	 */
	public JPanel getToolPanel() {
		JButton bt1 = new JButton("添加");
		JButton bt2 = new JButton("删除学生");
		JButton bt3 = new JButton("修改");
		JButton bt4 = new JButton("查询");
		JButton bt5 = new JButton("删除宿舍");
		bt1.setPreferredSize(new Dimension(110, 40));
		bt1.setFont(new Font("微软雅黑", Font.BOLD, 18));
		bt2.setPreferredSize(new Dimension(110, 40));
		bt2.setFont(new Font("微软雅黑", Font.BOLD, 18));
		bt3.setPreferredSize(new Dimension(110, 40));
		bt3.setFont(new Font("微软雅黑", Font.BOLD, 18));
		bt4.setPreferredSize(new Dimension(110, 40));
		bt4.setFont(new Font("微软雅黑", Font.BOLD, 18));
		bt5.setPreferredSize(new Dimension(110, 40));
		bt5.setFont(new Font("微软雅黑", Font.BOLD, 18));
		bt1.addActionListener(controller);
		bt1.setActionCommand("add");//设置此按钮的动作命令
		bt2.addActionListener(controller);
		bt2.setActionCommand("delete");
		bt3.addActionListener(controller);
		bt3.setActionCommand("update");
		bt4.addActionListener(controller);
		bt4.setActionCommand("findByStudentId");
		bt5.addActionListener(controller);
		bt5.setActionCommand("deleteRoom");
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(bt1);
		buttonPanel.add(bt2);
		buttonPanel.add(bt3);
		buttonPanel.add(bt4);
		buttonPanel.add(bt5);
		return buttonPanel;
	}
	/**
	 * 重新加载数据表格面板
	 */
	public void refresh(List<Student> studentList) {
		this.remove(tablePanel);//从容器中删除指定的组件
		this.add(this.getBookTablePanel(controller.getContactsService().findAll()), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}
	
	/**
	 * 
	 * 重新加载树组件
	 */
	public void refreshTree(List<Room> roomNameList) {
		this.remove(jp);
		this.add(this.treeWin(controller.getContactsService().findAllRoom()), BorderLayout.WEST);
		this.validate();
	}
	/**
	 * @param studentList
	 * 下拉菜单中的返回所有人通讯录的方法
	 */
	public void findAll(List<Student> studentList) {
		this.remove(tablePanel);//从容器中删除指定的组件
		this.add(this.getBookTablePanel(controller.getContactsService().findAll()), BorderLayout.CENTER);
		this.validate();
	}
	/**
	 * 查询结果
	 */
	public void showSearchResult(List<Student> result) {
		this.remove(tablePanel);
		this.add(this.getBookTablePanel(result), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}
	public JTree getTree() {
		return tree;
	}

	public void setController(Controller controller) {
		this.controller = controller;
	}
	public List<Room> getRoomNameList() {
		return roomNameList;
	}

}
